// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-6-29 11:26:17
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Login.java

package com.creawor.hz_market.user_manage;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.creawor.hz_market.bean.UserDetails;
import com.creawor.hz_market.t_role.t_role;
import com.creawor.hz_market.t_user.t_user;
import com.creawor.hz_market.t_user.t_user_QueryMap;
import com.creawor.hz_market.util.LoginUtils;
import com.creawor.imei.base.BaseAction;

public class Login extends BaseAction
{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Login.class);

    public Login()
    {
    }

    public String doList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws Exception
    {
		if (logger.isDebugEnabled()) {
			logger
					.debug("doList(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - start");
		}

        try {
					String strUsername = request.getParameter("username");
					String strPassword = request.getParameter("password");
					t_user_QueryMap query_map = new t_user_QueryMap();
					t_user user = query_map.getByUsername(strUsername);
					if(user != null && user.getPassword().equals(strPassword))
					{
					    List roleNames = new ArrayList();
					    for(Iterator it = user.getRoles().iterator(); it.hasNext(); roleNames.add(((t_role)it.next()).getRole_name()));
					    UserDetails userDetails = new UserDetails(user.getId(),user.getUsername(),user.getUsercode() ,user.getDept_code(),user.getDepartment(), roleNames);
					    userDetails.setUser(user);
					    LoginUtils.saveLoginUser(request, userDetails);

						if (logger.isDebugEnabled()) {
					logger
							.debug("doList(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - end");
				}
					    return "list";
					} else
					{
					    request.getSession().setAttribute("errors", "\u7528\u6237\u540D\u6216\u5BC6\u7801\u9519\u8BEF\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55.");

						if (logger.isDebugEnabled()) {
					logger
							.debug("doList(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - end");
				}
					    return "login";
					}
				} catch (RuntimeException e) {
			logger
					.error(
							"doList(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)",
							e);

					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			if (logger.isDebugEnabled()) {
			logger
					.debug("doList(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse) - end");
		}
				return null;
    }
}